package com.share.PACManager;

import java.util.ArrayList;
import java.util.List;

import com.share.PACManager.R;
import com.share.PACManager.model.CodeModel;
import com.share.PACManager.utils.Utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class SelectNativeActivity extends BindActivity {

	private ListView search_result_list;
	private BaseAdapter mItemAdapter;
	private List<CodeModel> mCodes; 
	
	private View key_word_layout;
	private View btn_search;
	private EditText key_word;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.select_native_layout);

        View btn_back = findViewById(R.id.id_btn_back);
		btn_back.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				finish();
			}			
		});
		
        btn_search = findViewById(R.id.id_btn_search);
        btn_search.setVisibility(View.GONE);
        btn_search.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				if(key_word_layout.getVisibility() == View.VISIBLE)
					key_word_layout.setVisibility(View.GONE);
				else
					key_word_layout.setVisibility(View.VISIBLE);
			}        	
        });        
        key_word_layout = findViewById(R.id.id_key_word_layout);
        key_word_layout.setVisibility(View.GONE);
        
        key_word = (EditText)findViewById(R.id.id_key_word);
        search_result_list = (ListView) findViewById(R.id.id_search_result_list);
        search_result_list.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				CodeModel code = mCodes.get(arg2);
				Intent intent = new Intent();
				intent.putExtra("code_info", code);
				setResult(RESULT_OK, intent);
				finish();
			}        	
        });
        
        mCodes = new ArrayList<CodeModel>();
        mItemAdapter = new ItemAdapter();        
        search_result_list.setAdapter(mItemAdapter);
        showList();
    }
    
    private void showList(){
    	String str = key_word.getText().toString();
    	if(Utils.isEmptyString(str))
    	{
    		mCodes = mGlobal.mCodeNative;
    		mItemAdapter.notifyDataSetChanged();
    	}
    }
    
    private class ItemAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return mCodes.size();
		}

		@Override
		public Object getItem(int position) {
			return mCodes.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null)
			{
				LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.native_list_item, null, false);
			}
			if(position % 2 == 0)
				convertView.setBackgroundColor(Color.TRANSPARENT);
			else
				convertView.setBackgroundColor(Color.parseColor("#55CCCCCC"));
			
			CodeModel code = (CodeModel) getItem(position);
			TextView people_native = (TextView)convertView.findViewById(R.id.id_text_people_native);
			people_native.setText(code.Value);
			return convertView;
		}    	
    };
}